<?php
/*
	Call by common.php
	YOHO Common functions
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

//读取数据调用(uch2.0/admincp_block.php)
function get_block($bname) {
	global $_SGLOBAL;

	$block = array();
	if(!empty($bname)) {
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('block')." WHERE blockname='$bname' LIMIT 1");
		$block = $_SGLOBAL['db']->fetch_array($query);
	}
	return $block;
}

//重写数据调用(foolant)
function add_block($bname,$setarr){
	$block = get_block($bname);
	if($setarr['perpage']) $setarr['num'] = 0;
	$setarr['blockname'] = $bname;	
	$setarr['cachename'] = $bname;
	//$setarr['htmlcode'] = addslashes(preg_replace("/href\=\"(?!http\:\/\/)(.+?)\"/i", 'href="'.getsiteurl().'\\1"', stripslashes($setarr['htmlcode'])));
	if($block) {
		updatetable('block', $setarr, array('blockname'=>$bname));
	} else {
		$bid = inserttable('block', $setarr, 1);
	}
	//更新缓存
	include_once(S_ROOT.'./source/function_cache.php');
	block_cache();
	
	$block = get_block($bname);
	
	//写入模板
	if($block['blocksql']) {
		if(empty($setarr['perpage'])) {
			$perstr = '';
			if(empty($setarr['num'])) $setarr['num'] = 1;
			$block['blocksql'] .= " LIMIT $setarr[startnum],$setarr[num]";
		} else {
			$perstr = 'perpage/'.$setarr['perpage'].'/';
		}
		$setarr['htmlcode'] = "<!--{block/{$perstr}sql/".rawurlencode($block['blocksql'])."/cachename/$setarr[cachename]/cachetime/$setarr[cachetime]}-->\r\n".stripslashes($setarr['htmlcode']);
	}
	$tpl = S_ROOT.'./data/blocktpl/'.$bname.'.htm';
	swritefile($tpl, $setarr['htmlcode']);
}

function add_table($tname, $fields){
	$charset = str_replace('-', '', $_SC['charset']);
	$extend = $_SGLOBAL['db']->version() > '4.1' ? " DEFAULT CHARSET={$charset} " : "";
	$sql[] = "CREATE TABLE IF NOT EXISTS `uchome_{$tname}` ({$fields}) ENGINE=MyISAM {$extend};";
	foreach($sql as $val){
		$_SC['tablepre'] != 'uchome_' && $val = str_replace("`uchome_", '`'. $_SC['tablepre'], $val);
		$_SGLOBAL['db']->query($val);
	}
}

?>